#include<iostream>
using namespace std;
#include<vector>
#include<queue>
class Solution {
public:
    class cmp {
    public:
        bool operator()(pair<int, int>& a1, pair<int, int>& a2) {
            return a1.first + a1.second < a2.first + a2.second;
        }
    };
    vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
        priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> que;
        int a = min(k, (int)nums1.size());
        for (int i = 0; i < a; ++i) {
            int b = min(k - i, (int)nums2.size());
            for (int j = 0; j < b; ++j) {
                que.push({ nums1[i],nums2[j] });
                if (que.size() > k)
                    que.pop();
            }
        }
        vector<vector<int>> ans;
        while (!que.empty()) {
            ans.push_back({ que.top().first,que.top().second });
            que.pop();
        }
        return ans;
    }
};